package practica1.ui;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import javax.swing.ComboBoxModel;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;

/**
 *
 * @author Miguel González - Ceura
 */
public class BarraEstadoModel implements ComboBoxModel {

    private LinkedList<String> lstDatos;
    private String selectedItem;
    private ArrayList<ListDataListener> listeners;
    
    public BarraEstadoModel() {
        lstDatos = new LinkedList<String>();
        selectedItem = "";
        listeners = new ArrayList<ListDataListener>();
    }
    
    public BarraEstadoModel(String[] datos) {
        lstDatos = new LinkedList<String>();
        lstDatos.addAll(Arrays.asList(datos));
        if(datos != null) {
            if(datos.length > 0) {
                selectedItem = datos[0];
            } else {
                selectedItem = "";
            }
        } else {
            selectedItem = "";
        }
        listeners = new ArrayList<ListDataListener>();
    }
    
    public void addElement(String dato) {
        if(lstDatos.size() == 0) {
            selectedItem = dato;
        }
        lstDatos.add(dato);
        int index = lstDatos.indexOf(dato);
        fireContentChanged(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED,
                index, index));
    }
    
    public void removeElement(String dato) {
        int index = lstDatos.indexOf(dato);
        
        if(lstDatos.indexOf(selectedItem) == index) {
            if(index == 0 && lstDatos.size() > 1) {
                selectedItem = lstDatos.get(1);
            } else if(index > 0) {
                selectedItem = lstDatos.get(index - 1);
            } else {
                selectedItem = "";
            }
            
        }
        lstDatos.remove(dato);
        fireContentChanged(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED,
                index, index));
    }
    
    public void removeElementAt(int index) {
        if(lstDatos.indexOf(selectedItem) == index) {
            if(index == 0 && lstDatos.size() > 1) {
                selectedItem = lstDatos.get(1);
            } else if(index > 0) {
                selectedItem = lstDatos.get(index - 1);
            } else {
                selectedItem = "";
            }
            
        }
        lstDatos.remove(index);
        
        fireContentChanged(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED,
                index, index));
    }
    
    public void setElementAt(int index, String dato) {
        if(lstDatos.indexOf(selectedItem) == index) {
            selectedItem = dato;
        }
        lstDatos.set(index, dato);
        
        fireContentChanged(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED,
                index, index));
    }
    
    private void fireContentChanged(ListDataEvent evt) {
        for(ListDataListener l : listeners) {
            l.contentsChanged(evt);
        }
    }
    
    @Override
    public int getSize() {
        return lstDatos.size();
    }

    @Override
    public Object getElementAt(int index) {
        return lstDatos.get(index);
    }

    @Override
    public void setSelectedItem(Object anItem) {
        selectedItem = anItem.toString();
    }

    @Override
    public Object getSelectedItem() {
        return selectedItem;
    }

    @Override
    public void addListDataListener(ListDataListener l) {
        if(l != null) {
            listeners.add(l);
        }
    }

    @Override
    public void removeListDataListener(ListDataListener l) {
        listeners.remove(l);
    }
    
}
